//
//  YDYNormalRefresh.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/10/21.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYNormalRefresh.h"
@interface YDYNormalRefresh()
/** 刷新之前GIF数组 */
@property (nonatomic, strong) NSArray *gifArray;
/** 箭头 */
@property (nonatomic, strong) UIImageView *arrawImgV;
/** 文字 */
@property (nonatomic, strong) UILabel *titleL;
/** 菊花 */
@property (nonatomic, strong) UIActivityIndicatorView *loadingV;
/** 用于显示GIF */
@property (nonatomic, strong) UIImageView *logoImgV;

@end

@implementation YDYNormalRefresh



-(void)prepare{
    [super prepare];
    
    self.mj_h = 88;
    //gif
    UIImageView *logoImgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"refresh_logo_1"]];
    logoImgV.contentMode = UIViewContentModeScaleAspectFit;
    self.logoImgV = logoImgV;
    [self addSubview:logoImgV];
    
    //title
    UILabel *titleL = [[UILabel alloc]init];
    titleL.textColor = [UIColor blackColor];
    titleL.textAlignment = NSTextAlignmentCenter;
    titleL.text = @"再拉，再拉就刷新给你看";
    titleL.hidden = NO;
    titleL.font = [UIFont systemFontOfSize:14];
    [titleL sizeToFit];
    self.titleL = titleL;
    [self addSubview:titleL];
    
    //箭头
    UIImageView *arraw = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow"]];
    arraw.contentMode = UIViewContentModeScaleAspectFit;
    self.arrawImgV = arraw;
    [self addSubview:arraw];
    
    //菊花
    UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.loadingV = loading;
    [self addSubview:loading];
    
    
    
}

-(void)placeSubviews{
    [super placeSubviews];
    
    self.logoImgV.centerX = self.mj_w * 0.5;
    self.logoImgV.top = 6;
    
    self.arrawImgV.top = self.logoImgV.bottom -2;
    self.arrawImgV.right = self.logoImgV.left + 14;
    
    self.loadingV.center = self.arrawImgV.center;
    
    self.titleL.left = self.arrawImgV.right + 2;
    self.titleL.top = self.logoImgV.bottom + 6;
}


#pragma mark -
#pragma mark -监听下拉状态的改变
//监听下拉offset
-(void)scrollViewContentOffsetDidChange:(NSDictionary *)change{
    [super scrollViewContentOffsetDidChange:change];
    
    if (self.state == MJRefreshStateIdle) {
        CGPoint offset = [change[@"new"] CGPointValue];
        
        if (offset.x == 0) {
            self.titleL.text = @"再拉，再拉就刷新给你看";
            [self.titleL sizeToFit];
        }
    }
    
}
-(void)setState:(MJRefreshState)state{
    MJRefreshCheckState;
    
    switch (state) {
            //回到闲置状态
        case MJRefreshStateIdle:{
            if (oldState == MJRefreshStateRefreshing) {
                self.arrawImgV.transform = CGAffineTransformIdentity;
                self.titleL.text = @"刷呀刷呀，刷完啦，喵^ω^";
                [self.titleL sizeToFit];
                [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
                    self.loadingV.alpha = 0.0;
                } completion:^(BOOL finished) {
                    //执行完成之后防止不是闲置状态
                    if (self.state !=MJRefreshStateIdle) return;
                    //恢复初始状态
                    [self.loadingV stopAnimating];
                    self.loadingV.alpha = 1.0;
                    [self.logoImgV stopAnimating];
                    self.arrawImgV.hidden = NO;
                    
                }];
                
            }else{
                //不是从刷新状态恢复闲置状态的情况(拉得太短)
                self.titleL.text = @"再拉，再拉就刷给你看";
                [self.titleL sizeToFit];
                [self.loadingV stopAnimating];
                [self.logoImgV stopAnimating];
                self.arrawImgV.hidden = NO;
                [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
                    self.arrawImgV.transform = CGAffineTransformIdentity;
                }];
            }
            
            break;
        }
            
        case MJRefreshStatePulling:{
            //拉的距离足够了
            [self.loadingV stopAnimating];
            self.arrawImgV.hidden = NO;
            self.titleL.text = @"够了啦，松开人家啦";
            [self.titleL sizeToFit];
            [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
                self.arrawImgV.transform = CGAffineTransformMakeRotation(M_PI);
            }];
            break;
            
        }
            
            
        case MJRefreshStateRefreshing:{
            self.loadingV.alpha = 1.0 ;//防止CASE1 动画未完成
            self.arrawImgV.hidden = YES;
            self.logoImgV.animationImages = self.gifArray;
            
            self.titleL.text = @"刷呀刷呀，好累啊，喵^ω^";
            [self.titleL sizeToFit];
            
            [self.loadingV startAnimating];
            [self.logoImgV startAnimating];
        }
        default:
            
            break;
    }
    
    
    
}
#pragma mark -
#pragma mark -lazy loading
-(NSArray *)gifArray{
    if (_gifArray.count == 0) {
        NSMutableArray *images = [NSMutableArray arrayWithCapacity:4];
        for (int i = 1; i < 5; i++) {
            
            UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"refresh_logo_%d",i]];
            [images addObject:image];
        }
        _gifArray = images;
    }
    
    return _gifArray;
}

@end
